<?php
/**
 * @package Joomla
 * @subpackage Jmonoslideshow
 * @copyright (C) 2010 - Matthieu BARBE - www.ccomca.com
 * @license GNU/GPL v2
 * 
 * Jmonoslideshow is a Joomla extension for Monoslideshow.
 *
 * Jmonoslideshow uses :
 * Monoslideshow (http://www.monoslideshow.com/)
 * Plupload (http://www.plupload.com/) by Moxicode Systems
 * json class by Michal Migurski, Matt Knapp, Brett Stimmerman
 * jquery a JavaScript Library (http://jquery.com/)
 * getid3 library (http://getid3.sourceforge.net/)
 * swfobject is an easy-to-use and standards-friendly method to embed Flash content, which utilizes one small JavaScript file (http://code.google.com/p/swfobject/)
 * Chained Selects jQuery Plugin (http://www.appelsiini.net/2010/jquery-chained-selects)
 *
 * Jmonoslideshow is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

// no direct access
defined( '_JEXEC' ) or die( 'Restricted access' );


/**
 * Album Table class
 *
 * @package    Joomla.Tutorials
 * @subpackage Components
 */
class JmonoslideshowTableAlbum extends JTable
{
	
	
	/**
	 * Constructor
	 *
	 * @param object Database connector object
	 */
	public function __construct(&$db) {
		parent::__construct('#__monoslideshow_album', 'id', $db);
	}
	
	protected function _getAssetName() {
		$k = $this->_tbl_key;
		return 'com_jmonoslideshow.album.'.(int) $this->$k;
	}
	
	/**
	 * Method to return the title to use for the asset table.
	 *
	 * @return	string
	 *
	 * @since	2.0
	 *
	 * @see		JTable::_getAssetTitle
	 */
	protected function _getAssetTitle() 
	{
		return $this->title;
	}
	
	/**
	 * Get the parent asset id for the record
	 *
	 * @return	int
	 *
	 * @since	2.0
	 *
	 * @see		JTable::_getAssetParentId
	 */
	protected function _getAssetParentId()
	{
		$asset = JTable::getInstance('Asset');
		$asset->loadByName('com_jmonoslideshow.category');
		return $asset->id;
	}
	/**
	* Overloaded bind function
	*
	* @acces public
	* @param array $hash named array
	* @return null|string	null is operation was satisfactory, otherwise returns an error
	* @see JTable:bind
	* @since 1.5
	*/
	function bind($array, $ignore = '')
	{
		if (key_exists( 'params', $array ) && is_array( $array['params'] ))
		{
			$registry = new JRegistry();
			$registry->loadArray($array['params']);
			$array['params'] = $registry->toString();
		}

		return parent::bind($array, $ignore);
	}
	
	 function check()
	{
	   if(  $this->catid == 0 ) {
			
		    $this->setError( JText::_('Please select a category') );
			return false;
			
		}
		
		if(  $this->title == "" ) {
			
		    $this->setError( JText::_('You must enter a title') );
			return false;
			
		}
		
		$user =& JFactory::getUser();
		
		
		$this->modified_by = $user->get('id');
		$this->modified = date('Y-m-d H:i:s');
		
		
		if ( !$this->id ) {	
        	$this->author = $user->get('id');
			$this->created = date('Y-m-d H:i:s');
			// if new item, order last in appropriate group
			$where = 'catid = ' . (int) $this->catid ;
			$this->ordering = $this->getNextOrder( $where );
			$this->published = 1;
        }
		
		else
		{
			if ($this->catid > 0) {
					$db		 =& JFactory::getDBO();
					$query = 'UPDATE #__monoslideshow'
							.' SET catid = '.$this->catid
							.' WHERE albumid = '.$this->id
					;
					$db->setQuery( $query );
					$db->query();
				}
		}
		
		return true;
	}
	
		public function store($updateNulls = false) 
	{
		$date = JFactory::getDate();
		$user = JFactory::getUser();
		$db = JFactory::getDbo();
		if ($this->id) 
		{
			// Existing item
			$this->modified = $date->toMySQL();
			$this->modified_by = $user->get('id');
			$table = JTable::getInstance('Album', 'JmonoslideshowTable');
			$table->load($this->id);
			$change_ordering = $table->catid != $this->catid;
		}
		else
		{
			// New entry. An entry created and created_by field can be set by the user,
			// so we don't touch either of these if they are set.

			if (!intval($this->created)) 
			{
				$this->created = $date->toMySQL();
			}
			if (empty($this->author)) 
			{
				$this->author = $user->get('id');
			}
			$change_ordering = true;
		}
		if ($change_ordering) 
		{
			// Set the ordering to be at the end of the catagory
			$query = $db->getQuery(true);
			$query->select('MAX(ordering)');
			$query->from('#__monoslideshow_album');
			$query->where('id=' . (int)$this->id);
			$db->setQuery($query);
			$max = $db->loadResult();
			$this->ordering = $max + 1;
		}

		// Attempt to store the data.
		if (parent::store($updateNulls)) 
		{
			if (isset($table)) 
			{
				$table->reorder('id=' . $table->id);
			}
			return true;
		}
		return false;
	}
}
?>